'use strict';

module.exports = class extends think.Model {
    async login(param) {
        return this.where({ username: param.username, password: think.md5(param.password), status: '1' }).find();
    }

    async pageData(param) {
        let sql = this.field("id,role_name").where({ 'id': ['!=', 1] }).page(param.current);
        if (!think.isEmpty(param.name)) {
            sql = sql.where({ 'role_name': ['like', '%' + param.name + '%'] });
        }
        let data = await sql.countSelect();
        return data;
    }

    async updateData(param) {

        await this.where({ 'id': param.id }).update(param);
    }

    async addData(param) {
        let count = await this.count('id');
        param.id = count + 1;
        param.create_data = think.datetime();
        param.password = think.md5('111111');
        param.del_flag = 0;
        param.status = 1;
        if (param.role_id == 1) {
            param.rolename = '管理员';
        } else if (param.role_id == 2) {
            param.rolename = '用户';
        }
        think.logger.info(param);
        await this.add(param);
    }
};